//
//  MsgTableView.m
//  GameBaby
//
//  Created by iOS on 14-2-27.
//  Copyright (c) 2014年 keyZhang. All rights reserved.
//

#import "MsgTableView.h"
#import "MsgTableViewCell.h"
#import "WebShowViewController.h"
#import "DataService.h"
#import "MsgModel.h"
@implementation MsgTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (void)awakeFromNib
{
    [super awakeFromNib];
    self.rowHeight = 62;
    self.dataSource = self;
    self.delegate =self;

}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSLog(@"%d",self.data.count);
//    return self.data.count;
//    return 7;
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
    MsgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MsgTableViewCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else
    {
        return cell;
    }
    cell.model = self.dataList[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebShowViewController *wbVC = [[WebShowViewController alloc] init];
    wbVC.linkURL = [(MsgModel *)self.dataList[indexPath.row] url] ;
        wbVC.contentTitle = @"消息";
    [self.viewController.navigationController pushViewController:wbVC animated:YES];
//    标记为已读
    MsgModel *danModel = (MsgModel *)[self.dataList objectAtIndex:indexPath.row];
    BOOL alread = (BOOL)[danModel.isread intValue];
    if (!alread) {
        [danModel setValue:@"1" forKey:@"isread"];
    }else
    {
        [danModel setValue:@"1" forKey:@"isread"];
    }
    [self reloadData];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        删除操作
        MsgModel *msgModel = self.dataList[indexPath.row];
        NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/apply/msg_del"];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",msgModel.msgid,@"msgid",nil];
        __weak MsgTableView *this = self;
        [DataService requestWithURL:urlStr
                             params:dic
                         httpMethod:@"POST"
                        finishBlock:^(id result) {
                            [this loadDeleteFinished:result];
                        }];
        
        [self.dataList removeObjectAtIndex:indexPath.row];
        // Delete the row from the data source.
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
        [tableView endUpdates];
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

- (void)loadDeleteFinished:(NSDictionary *)result
{
}

@end
